package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBase;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialBaseHisDetail;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBaseHis;


/**材料使用管理-库存管理-结转信息
 * <p>Title: ServiceZMCM91.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 杨鸣
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-30 上午09:49:43  杨鸣  Create
 *</pre>
 */

public class ServiceZMCM91 extends ServiceEPBase {

	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30 上午09:49:43  杨鸣
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		outInfo.addBlock(User.getInstance().getConstructionStaff(true)); //addBlock的名字是 "cmes.cs.constructionStaff",值是 "empCode",显示是 "empName"
		outInfo.set("userId", UserSession.getUserId());//当前登录用户ID
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30 上午09:49:43  杨鸣
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBaseHis.query",new ZMCMMaterialBaseHis());
		//此处需要修改
		//outInfo.addBlock(User.getConstructionStaff(true)); //"cmes.cs.constructionStaff", "empCode", "empName"
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:07:00 杨鸣
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验同一类别下没有重复的 ，且物资编码没有重复
		Map<String, String> params = new HashMap<String, String>();
		String fdYear = inInfo.getBlock("result").getCellStr(0, "fdYear");	
		int fdMonth = Integer.parseInt(inInfo.getBlock("result").getCellStr(0, "fdMonth"));			
		params.put("fdYear", fdYear);
		params.put("fdMonth", fdMonth+"");
		boolean isRepeat = validate(inInfo, "ZMCMMaterialBaseHis.validate", params);
		if(isRepeat) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_MATERIALBASEHIS"));//主键
			inInfo.getBlock("result").setCell(0, "fdPeople", UserSession.getUserId());//当前登录人ID
			inInfo.getBlock("result").setCell(0, "fdMonth", fdMonth+"");//月份
			EiInfo outInfo = super.insert(inInfo, "ZMCMMaterialBaseHis.insert");
			
			//结转时修改库存所有数据的结转年月
			boolean updateYearMonthForHis = MaterialBase.UpdateYearMonthForHis(fdYear,fdMonth+"");
			if(!updateYearMonthForHis) {
				outInfo.setStatus(-1);
				outInfo.setMsg("库存数据的结转失败！");
				return outInfo;
			}else{
				//复制明细到结转
				boolean insertHisDetailFromBase = MaterialBaseHisDetail.InsertHisDetailFromBase();
				if(!insertHisDetailFromBase) {
					outInfo.setStatus(-1);
					outInfo.setMsg("生成结转数据明细失败！");
					return outInfo;
				}else{
					return outInfo;
				}
			}
		}
	}

	
	/**
	 * 结转年月重复验证
	 * <p>Function: validateNo</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:13:00 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");

		params.put("fdKeyId", fdKeyId);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer("提交失败，");
			msg.append("结转年份、结转月份重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
}
